package com.financing.platform.web.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.FpProductDeepAnalyticsDTO;
import com.financing.platform.dto.FpProjectTransferHisDTO;
import com.financing.platform.dto.ProjectDTO;
import com.financing.platform.dto.UserInfoDTO;
import com.financing.platform.service.IFpProductDeepAnalyticsService;
import com.financing.platform.service.IFpProjectTransferHisService;
import com.financing.platform.service.IProjectService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.StringUtil;

/**
 * @Description: 产品深度分析 Controller
 * @author lisu
 * @version 1.0
 * @created 
 */
@Controller
@RequestMapping("/api/product")
@Scope("prototype")
public class FpProductDeepAnalyticsController  extends BaseAction {
	@Autowired
	private IFpProductDeepAnalyticsService fpProductDeepAnalyticsServiceImpl;
	@Autowired
	private IFpProjectTransferHisService fpProjectTransferHisService;
	@Autowired
	private IProjectService projectServiceImpl;
	
	@RequestMapping("/deepAnalyticsSave.shtml")
	@ResponseBody
	public ResultMap deepAnalyticsSave(HttpServletRequest request,
			HttpServletResponse response,FpProductDeepAnalyticsDTO dto) {
		try{
			log.info("Begin deepAnalyticsSave !");
			
			/*//判断是否有权限修改产品
			if(!isAccess(dto.getFpProductId(),request)){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_NO_ACCESS);
				return resultMap;
			}*/
			DTOdateSet.setUpdateInfo(dto, request.getSession());
			fpProductDeepAnalyticsServiceImpl.saveFpProductDeepAnalyticsDTO(dto);
			FpProductDeepAnalyticsDTO  bean = fpProductDeepAnalyticsServiceImpl.getFpProductDeepAnalytics(dto);
			fpProductDeepAnalyticsServiceImpl.saveFpProductDeepAnalyticsHisDTO(bean);
			resultMap.setAttribute("dto", bean);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("End deepAnalyticsSave!");
		}catch(Exception e){
			log.error("deepAnalyticsSave error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			
			return resultMap;
	}
	
	@RequestMapping("/deepAnalyticsDetail.shtml")
	@ResponseBody
	public ResultMap deepAnalyticsDetail(HttpServletRequest request,
			HttpServletResponse response,FpProductDeepAnalyticsDTO dto) {
		try{
			log.info("Begin deepAnalyticsDetail info !");
			dto = fpProductDeepAnalyticsServiceImpl.getFpProductDeepAnalytics(dto);
			resultMap.setAttribute("dto", dto);
		    resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			log.info("End deepAnalyticsDetail info !");
		}catch(Exception e){
			log.error("deepAnalyticsDetail info error!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
	}
	/**
	 * 判断要修改的产品是否为当前用户所有
	 * @param projectId
	 * @param request
	 * @return
	 */
	private boolean isAccess(int projectId,HttpServletRequest request){
		UserInfoDTO user = (UserInfoDTO) request.getSession().getAttribute(DTOdateSet.user);
		//超级管理员具有所有产品的管理权限
		if(user.getUsername().equals("admin")){
			return true;
		}
		
		//权限扭转接收人
		FpProjectTransferHisDTO fpPTHis = new FpProjectTransferHisDTO();
		fpPTHis.setId(projectId);
		String createBy = fpProjectTransferHisService.getTransferUserNameByProjectID(fpPTHis);
		if(!StringUtil.isNullOrEmptyStr(createBy)){
			//是否是扭转接收人
			if(user.getUsername().equals(createBy)){
				return true;
			}else{
				return false;
			}
		}

		ProjectDTO p = new ProjectDTO();
			p.setId(projectId);
			 p = projectServiceImpl.queryProject(p);
			 if(p!=null && p.getCreateBy().equals(user.getUsername())){
				 return true;
			 }
		return false;
	}
	
}